<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="resources/templates/mainTemplate.xhtml">

    <ui:param name="pagetitle" value="CHUMS - Manage Institutions"/>

    <ui:define name="centerContent" class="turquaz">   

        <p:ajaxStatus onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>  

        <p:dialog appendToBody="true" resizable="false" modal="true" widgetVar="statusDialog" header="Loading"   
                  draggable="false" closable="false">  
            <p:graphicImage value="resources/images/ajax-loader.gif" />
        </p:dialog> 



        <center>
            <h:form id="formAllInstitutions" prependId="false" >
                <p:fieldset legend="Manage Institutions">  
                    <h:panelGrid columns="1"  cellpadding="10"> 
                        <p:growl id="growlAllInstitutions"  showDetail="true" showSummary="false" life="5000" redisplay="false" />  
                        <p:dataTable id="allInstitutionsTableID" 
                                     var="institution" 
                                     value="#{adminBean.allInstitutions}" 
                                     paginator="true" 
                                     rows="5"
                                     rowsPerPageTemplate="5,10,15"
                                     rowKey="#{institution.id}"
                                     emptyMessage="No Rows"
                                     editable="true">

                            <p:ajax event="rowEdit" update=":formAllInstitutions:allInstitutionsTableID :formAllInstitutions:growlAllInstitutions" listener="#{adminBean.onEditAllInstitutionsRow}"></p:ajax>
                            <p:ajax event="filter" listener="#{adminBean.onFilterAllWebResources}" update = ":formAllInstitutions:allInstitutionsTableID"/>

                            <f:facet name="header">
                                <h:outputText value="All Institutions" />
                            </f:facet>

                            <p:column sortBy="#{institution.id}" 
                                      headerText="ID" 
                                      width="40"
                                      filterBy="#{institution.id}"  
                                      filterMatchMode="contains"
                                      >

                                <f:facet name="header">
                                    <h:outputText value="ID" />
                                </f:facet>

                                <h:outputText value="#{institution.id}" />
                            </p:column>

                            <p:column sortBy="#{institution.url}" 
                                      headerText="URL" 
                                      width="60"
                                      filterBy="#{institution.url}"  
                                      filterMatchMode="contains"
                                      >

                                <f:facet name="header">
                                    <h:outputText value="URL" />
                                </f:facet>

                                <h:outputText value="#{institution.url}" />
                            </p:column>

                            <p:column sortBy="#{institution.title}" 
                                      headerText="Title" 
                                      width="100"
                                      filterBy="#{institution.title}"  
                                      filterMatchMode="contains"
                                      >

                                <f:facet name="header">
                                    <h:outputText value="Title" />
                                </f:facet>

                                <p:cellEditor>  
                                    <f:facet name="output">  
                                        <h:outputText value="#{institution.title}" />  
                                    </f:facet>  
                                    <f:facet name="input">  
                                        <p:inputText id="title" value="#{institution.title}"  style="width:100%">
                                        </p:inputText>
                                    </f:facet>  
                                </p:cellEditor>  
                            </p:column>

                            <p:column sortBy="#{institution.description}" 
                                      headerText="Description" 
                                      width="100"
                                      filterBy="#{institution.description}"  
                                      filterMatchMode="contains"
                                      >

                                <f:facet name="header">
                                    <h:outputText value="Description" />
                                </f:facet>

                                <p:cellEditor>  
                                    <f:facet name="output">  
                                        <h:outputText value="#{institution.description}" />  
                                    </f:facet>  
                                    <f:facet name="input">  
                                        <p:inputText  id="description" value="#{institution.description}"  style="width:100%">
                                        </p:inputText>
                                    </f:facet>  
                                </p:cellEditor>  
                            </p:column>

                            <p:column sortBy="#{institution.averageRating}" 
                                      headerText="Average Rating" 
                                      width="60"
                                      filterBy="#{institution.averageRating}"  
                                      filterMatchMode="contains"
                                      >

                                <f:facet name="header">
                                    <h:outputText value="Average Rating" />
                                </f:facet>

                                <h:outputText value="#{institution.averageRating}" />
                            </p:column>


                            <p:column
                                headerText="Update" 
                                width="50">  

                                <p:rowEditor />  
                            </p:column>

                            <p:column headerText="Delete" 
                                      >  

                                <p:commandButton style="align: center"  icon="ui-icon-close"  update=":formAllInstitutions:allInstitutionsTableID :formAllInstitutions:growlAllInstitutions"
                                                 oncomplete="confirmation.show()">
                                    <f:setPropertyActionListener value="#{institution}" target="#{adminBean.toBeRemovedWebResource}" />
                                </p:commandButton>

                            </p:column>  
                            
                            <f:facet name="footer">
                                <p:commandButton style="float: right;" id="institutionAddButton" value="Add Institution" icon="ui-icon-document"  
                                                 action="addInstitution.xhtml?faces-redirect=true"/>
                            </f:facet>

                        </p:dataTable>

                    </h:panelGrid>  

                </p:fieldset>
            </h:form> 
            <p:confirmDialog appendToBody="true" id="confirmDialog" message="Are you sure you want to delete this institution?" header="Delete Institution" severity="alert" widgetVar="confirmation">  
                <h:form>
                    <center>
                        <p:commandButton id="confirm" value="Yes" update=":formAllInstitutions:allInstitutionsTableID :formAllInstitutions:growlAllInstitutions" oncomplete="confirmation.hide();" action="#{adminBean.removeWebResource}" />  
                        <p:commandButton id="decline" value="No" onclick="confirmation.hide()" type="button" />   
                    </center>
                </h:form>
            </p:confirmDialog> 

        </center>

    </ui:define>


    <ui:define name="leftContent" class="turquaz">
        <ui:include src="managementLeftPanel.xhtml" />
    </ui:define> 


</ui:composition>

